package com.nf.demo.dao.impl;

import com.nf.demo.dao.HandlerDAO;
import com.nf.demo.model.pojo.Staffs;
import com.nf.demo.utils.CustomBeanProcessor;
import com.nf.demo.utils.DBUtils;
import com.nf.demo.utils.DruidUtils;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.RowProcessor;
import org.apache.commons.dbutils.handlers.*;

import java.sql.SQLException;
import java.util.*;

/**
 * @author lwj
 * @date 2025-02-27
 * @vesion 1.0.0
 */
public class HandlerDAOImpl implements HandlerDAO {
    QueryRunner run = new QueryRunner(DruidUtils.getDataSource());

    // 创建自定义 RowProcessor
    RowProcessor rowProcessor = new BasicRowProcessor(new CustomBeanProcessor());
    @Override
    public Map<String, Object> selectById(Staffs staffs) {
        // 定义 SQL 语句
        String sql = "select id,name,age,phone,sta_pos staPos,add_time addTime,update_time updateTime from staffs where id = ?";
        try{
            return run.query(sql,new MapHandler(rowProcessor),staffs.getId());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Map<String, Object>> selectAll() {
        String sql = "select id,name,age,phone,sta_pos staPos,add_time addTime,update_time updateTime from staffs";
        try{
            return run.query(sql,new MapListHandler(rowProcessor));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Staffs> selectAllList() {
        // 定义 SQL 语句
        String sql = "select id,name,age,phone,sta_pos staPos,add_time addTime,update_time updateTime from staffs";
        try{
            return run.query(sql,new BeanListHandler<>(Staffs.class,rowProcessor));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Staffs selectByIdList(Staffs staffs) {// 定义 SQL 语句
        String sql = "select id,name,age,phone,sta_pos staPos,add_time addTime,update_time updateTime from staffs where id = ?";
        try{
            return run.query(sql,new BeanHandler<>(Staffs.class,rowProcessor),staffs.getId());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String findSingleName(Staffs staffs) {
        // 定义 SQL 语句
        String sql = "select name from staffs where id = ?";
        try{
            // return run.query(sql,new BeanHandler<>(Staffs.class),staffs.getId()).getName();
            return run.query(sql,new ScalarHandler<>(),staffs.getId());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Long getRecordCount() {
        // 定义 SQL 语句
        String sql = "select count(id) count from staffs";
        try{
            return run.query(sql,new ScalarHandler<>());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<String> findAllName() {
        // 定义 SQL 语句
        String sql = "select name from staffs";
        try{
            return run.query(sql,new ColumnListHandler<>());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Object[] selectByIdObject(Staffs staffs) {
        // 定义 SQL 语句
        String sql = "select id,name,age,phone,sta_pos staPos,add_time addTime,update_time updateTime from staffs where id = ?";
        try{
            // return run.query(sql,new BeanHandler<>(Staffs.class,rowProcessor),staffs.getId());
            return run.query(sql,new ArrayHandler(rowProcessor),staffs.getId());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Object[]> selectAllObject() {
        // 定义 SQL 语句
        String sql = "select id,name,age,phone,sta_pos staPos,add_time addTime,update_time updateTime from staffs";
        try{
            return run.query(sql,new ArrayListHandler());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}
